/**
 *<p>Copyright: Copyright (c) 2012</p> 
 *<p>Company: SCPII</p>
 *<p>Project:scpii-mobi-page</p>
 *@author xiezhouyan
 *@Date 2013-4-27 下午5:19:40
 *@version 1.0
 *@description:
 */
package com.scpii.mobi.page.common.interceptor;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.opensymphony.xwork2.util.ValueStack;
import com.scpii.api.common.auth.token.DefaultRequestToken;
import com.scpii.api.common.auth.token.HeaderRquestToken;
import com.scpii.api.common.auth.token.RequestToken;
import com.scpii.api.common.auth.token.TokenService;
import com.scpii.api.common.exception.InvalidTokenException;
import com.scpii.api.domain.auth.UsersInfoDetails;
import com.scpii.mobi.page.action.flex.SubAction;
import com.scpii.mobi.page.domain.user.User;
import com.scpii.mobi.page.service.user.UserService;

public class TokenInterceptor extends AbstractInterceptor {

	private final static Log logger = LogFactory.getLog(TokenInterceptor.class);

	@Resource
	private UserService userService;

	@Resource
	private TokenService tokenService;

	private List<RequestToken> requestTokens;

	/**
	 * 
	 */
	private static final long serialVersionUID = -1026844835609673304L;

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		if (invocation.getAction() instanceof SubAction) {
			return invocation.invoke();
		}
		String tokenValue = parseToken(request);
		if (tokenValue == null) {
			if (logger.isDebugEnabled()) {
				logger.debug("No token in request, will continue chain.");
			}
			return invocation.invoke();

		}
		UsersInfoDetails usersInfoDetails = null;
		try {
			usersInfoDetails = tokenService.loadUsersInfoDetails(tokenValue);
		} catch (InvalidTokenException e) {
			if (logger.isDebugEnabled()) {
				logger.debug("token error " + e.getMessage());
			}
			return invocation.invoke();
		}
		if (usersInfoDetails == null) {
			return invocation.invoke();
		}
		User user = userService.selectByPrimaryKey(usersInfoDetails.getId());
		if (user == null) {
			return invocation.invoke();
		}
		final ValueStack stack = ActionContext.getContext().getValueStack();
		stack.setValue("user", user);
		return invocation.invoke();
	}

	protected String parseToken(HttpServletRequest request) {
		String token = null;
		logger.debug("parse token in  request parameters. begin ");
		for (RequestToken requestToken : requestTokens) {
			token = requestToken.getRequestToken(request);
			if (token != null) {
				break;
			}
		}
		if (token == null) {
			logger.debug("Token not found in request parameters.  Not an Auth request.");
		}
		logger.debug("parse token in  request parameters. end ");
		return token;
	}

	@Override
	public void init() {
		super.init();
		requestTokens = new ArrayList<RequestToken>();
		requestTokens.add(new DefaultRequestToken());
		requestTokens.add(new HeaderRquestToken());
	}
}
